<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.yuxin.common.util.DBUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="java.util.regex.Matcher"%>
<%@page import="com.yuxin.core.TransactionManager"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Date"%>

<%
	String action = request.getParameter("action"); // Request action
	String id = request.getParameter("id"); // Trader id
	String status = request.getParameter("status"); // Trader status
	Connection conn = null;
	TransactionManager tranM = null;
	String redirect = null;
	// Init trader list
	Map<String, Object> traderMap = null;
	try{		
		if(id != null && !"".equals(id)) {
			conn = DBUtil.getConnection();
			tranM = new TransactionManager(conn);
			tranM.begin();
			
			String sql = "select t.ID, t.NAME, t.LICENSE_IMG, t.LEGAL_IMG, t.CONTACT, t.PHONE, t.STATUS from p_trade t where t.ID = ?";
			traderMap = DBUtil.queryUniqueResult(conn, sql, id);
	
			String errMsg = "<div align='center'><div style='border:4px solid #F00; line-height:100px; width:300px; text-align:center; margin-top:100px;'><strong>[msg]</strong></div></div>";
			if(traderMap == null) {
				out.println(errMsg.replace("[msg]", "商家不存在！"));
				return;
			}
			
			if(status != null && !"".equals(status)) {
				Pattern p = Pattern.compile("[2,3]");
				Matcher m = p.matcher(status);
				if(!m.matches()) {
					out.println(errMsg.replace("[msg]", "错误的状态！"));
					return;
				}
				Map<String, Object> mapData = new HashMap<String, Object>(); {
					mapData.put("ID", id);
					mapData.put("STATUS", status);
				}
				sql = "update p_trade set STATUS = ?, UPDATE_TIME = ? where ID = ?";
				DBUtil.executeSql(conn, sql, new Object[]{status, new Date(), id});
				redirect = request.getContextPath()+"/admin/trader_audit.jsp";
			}
		}
	} catch (Exception ex) {
		redirect = request.getContextPath()+"/admin/trader_detail.jsp?id=" + id;
		tranM.rollback();
		ex.printStackTrace();
	} finally {
		if(tranM != null) {
			tranM.commit();
		}	
		DBUtil.close(conn);	
		if(redirect != null) {
			response.sendRedirect(redirect);
		}
	}
%>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="/meta.jsp" %>
    <title>${site_title}</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%@ include file="/header.jsp" %>
<div class="container" >
    <div class="row-fluid">
        <%@ include file="menu.jsp" %>
        <div class="span9">
            <form id="traderForm" name="traderForm" method="post">
                <table align="left" cellpadding="10" cellspacing="10" style="margin-top:20px;">
                    <tr>
                        <td align="left">公司名称：</td>
                        <td align="left"><%=traderMap.get("NAME") %></td>
                    </tr>
                    <tr>
                        <td align="left">负责人：</td>
                        <td align="left"><%=traderMap.get("CONTACT") %></td>
                    </tr>
                    <tr>
                        <td align="left">联系电话：</td>
                        <td align="left"><%=traderMap.get("PHONE") %></td>
                    </tr>
                     <tr>
                        <td align="left">状态：</td>
                        <td align="left"><%="1".equals(traderMap.get("STATUS")) ? "待审核" : "2".equals(traderMap.get("STATUS")) ? "审核通过" : "审核不通过" %></td>
                    </tr>
                    <tr>
                        <td align="left" valign="top">营业执照：</td>
                        <td align="left" valign="top"><img src="<%=request.getContextPath()%>/<%=traderMap.get("LICENSE_IMG") %>" width="60" height="60" /></td>
                    </tr>
                    <tr>
                        <td align="left" valign="top">法人头像：</td>
                        <td align="left" valign="top"><img src="<%=request.getContextPath()%>/<%=traderMap.get("LEGAL_IMG") %>" width="60" height="60" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                        	<%
								if(action != null && "audit".equals(action)) {
							%>
                            <input type="button" value="资料有误" style="width:150px" 
                                onClick="if(window.confirm('确定要执行此操作码？')){window.location.href='trader_detail.jsp?id=<%=traderMap.get("ID") %>&amp;status=3';}" />
                             &nbsp;&nbsp;   
                            <input type="button" value="验证有效" style="width:150px" 
                                onClick="if(window.confirm('确定要执行此操作码？')){window.location.href='trader_detail.jsp?id=<%=traderMap.get("ID") %>&amp;status=2';}" />                           <%
								}
							%>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
</div>
<script>
$(document).ready(function(){
	if("${param.action}" == "audit") {	
		$("#menu_trader_audit").addClass("active");
	} else {
		$("#menu_trader_management").addClass("active");
	}
});
</script>
<%@ include file="/footer.jsp" %>
</body>
</html>